package com.glorysoft.knowlespda.ui.home.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import android.view.View
import com.glorysoft.knowlespda.BR
import com.glorysoft.knowlespda.R
import com.glorysoft.knowlespda.base.BaseActivity
import com.glorysoft.knowlespda.base.Constant
import com.glorysoft.knowlespda.base.MyApp
import com.glorysoft.knowlespda.databinding.ActivityHomeBinding
import com.glorysoft.knowlespda.entity.HomeEntity
import com.glorysoft.knowlespda.ui.engineering.view.EngineeringActivity
import com.glorysoft.knowlespda.ui.eqpauthor.view.EqpAuthorizationActivity
import com.glorysoft.knowlespda.ui.home.viewmodel.HomeViewModel
import com.glorysoft.knowlespda.ui.login.view.LoginActivity
import com.glorysoft.knowlespda.ui.track.view.TrackActivity
import com.glorysoft.knowlespda.ui.track.view.TrackOutActivity
import com.glorysoft.knowlespda.ui.track.view.TracksferActivity
import com.glorysoft.knowlespda.ui.updatecount.view.UpdateCountActivity
import com.glorysoft.knowlespda.utils.PopWindowsUtils
import kotlinx.android.synthetic.main.activity_home.*
import java.util.*

/**
 * FileName: HomeActivity
 * Author: XJH
 * Date: 2021/7/12 17:19
 * Description:
 * History:
 */
class HomeActivity : BaseActivity<HomeViewModel, ActivityHomeBinding>(),
    View.OnClickListener {

    var regionPop: PopWindowsUtils.GeneralPop? = null
    var timer: Timer? = null
    var homeTimer: Timer? = null
    var task: TimerTask? = null
    var homeTask: TimerTask? = null

    private val handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            Log.e("TIMER", "run:  + 收到消息")
            if(tvRegion.text == "All"){
                viewModel.pageNo = 1
                viewModel.getNotification("All")
            }

            super.handleMessage(msg)
        }
    }

    override fun layoutId(): Int {
        return R.layout.activity_home
    }

    override fun initVariableId(): Int {
        return BR.mainViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        tvTrack.setOnClickListener(this)
        tvTrackOut.setOnClickListener(this)
        tvTracksfer.setOnClickListener(this)
        tvUpdate.setOnClickListener(this)
        tvExit.setOnClickListener(this)
        flRegion.setOnClickListener(this)
        tvCewa.setOnClickListener(this)
        tvEngineering.setOnClickListener(this)

        timer = Timer()
        task = MyApp.showTimer(timer)
        tvNo.text = Constant.PDA_NO
        tvUser.text = Constant.CURRENT_USER

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            rvNoticeList.setOnScrollChangeListener(View.OnScrollChangeListener setOnScrollChangeListener@{ view: View?, i: Int, i1: Int, i2: Int, i3: Int ->
                if (!rvNoticeList.canScrollVertically(1)) {
                    if (!rvNoticeList.canScrollVertically(-1)) {
                        return@setOnScrollChangeListener
                    }
                    viewModel.pageNo++
                    viewModel.getNotification(tvRegion.text.toString())
                }
            })
        }
    }

    override fun onRestart() {
        showTimer()
        super.onRestart()
    }

    override fun initData() {
        showTimer()

        viewModel.defUI.startActivity.observe(this){
            Constant.PDA_NO = ""
            startActivity(LoginActivity::class.java)
            finish()
        }

        viewModel.defUI.callObserve.observe(this){
            if (it.type == 0){
                val data = it.item as HomeEntity.NoticeEntity
                when {
                    data.noticetype.contains("ReplaceIn") -> {
                        val intent = Intent(this, TrackActivity::class.java)
                        intent.putExtra("DATA", data.machineid)
                        startActivity(intent)
                    }
                    data.noticetype.contains("ReplaceOut") -> {
                        val intent = Intent(this, TrackOutActivity::class.java)
                        intent.putExtra("DATA", data.machineid)
                        startActivity(intent)
                    }
                    else -> {
                        val intent = Intent(this, TracksferActivity::class.java)
                        intent.putExtra("DATA", data.machineid)
                        startActivity(intent)
                    }
                }

            }else{
                if (viewModel.homeData.isauth.isNotEmpty()){
                    tvAuth.text = "+" + viewModel.homeData.isauth
                }else{
                    tvAuth.text = ""
                }
            }

        }
    }


    private fun showTimer() {
        homeTimer = Timer()
        homeTask = object : TimerTask() {
            override fun run() {
                val message = Message()
                message.what = 1
                handler.sendMessage(message)
                Log.e("TIMER", "run:  + 发出消息")
            }
        }
        homeTimer!!.schedule(homeTask, Date(), 1000 * 60)
    }

    override fun onDestroy() {
        MyApp.stopTimer(task, timer)
        //关闭定时任务
        super.onDestroy()

    }

    override fun onPause() {
        //关闭定时任务
        homeTimer!!.cancel()
        homeTask!!.cancel()
        super.onPause()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tvTrack -> {
                startActivity(TrackActivity::class.java)
            }
            R.id.tvTrackOut -> {
                startActivity(TrackOutActivity::class.java)
            }
            R.id.tvTracksfer -> {
                startActivity(TracksferActivity::class.java)
            }
            R.id.tvEngineering -> {
                startActivity(EngineeringActivity::class.java)
            }
            R.id.tvUpdate -> {
                startActivity(UpdateCountActivity::class.java)
            }
            R.id.tvExit -> {
                viewModel.loginOut()
            }
            R.id.tvCewa -> {
                startActivity(EqpAuthorizationActivity::class.java)
            }
            R.id.flRegion -> {
                if (regionPop == null) {
                    regionPop = PopWindowsUtils.getOneChoosePop(
                        this,
                        flRegion.width,
                        viewModel.regionList,
                        IvRegion,
                        object : PopWindowsUtils.PopTextAndPositionCallback {
                            override fun onSuccess(string: String, potion: Int) {
                                tvRegion.text = string
                                viewModel.pageNo = 1
                                viewModel.getNotification(string)
                            }
                        }
                    )
                }

                if (regionPop!!.isShowing) {
                    regionPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(IvRegion)
                    regionPop?.showAsDropDown(flRegion, 0, 0)
                }
            }
        }
    }
}